package com.gitee.ricesugar.admin;

import com.gitee.ricesugar.admin.module.AddAdmin;
import com.gitee.ricesugar.admin.module.ListAdmin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class AdminPanel {
    private static JPanel contentPanel;

    public static JPanel createAdminPanel() throws IOException {
        //新建主布局
        JPanel adminPanel = new JPanel();
        adminPanel.setLayout(new BorderLayout());

        //添加侧边栏
        JPanel sidebar = new JPanel();
        sidebar.setLayout(new GridLayout(2, 1));

        JButton addAdminButton = new JButton("添加管理员");
        JButton adminListButton = new JButton("管理员列表");
        addAdminButton.setPreferredSize(new Dimension(96, 40));

        sidebar.add(addAdminButton);
        sidebar.add(adminListButton);

        //添加每个页面
        contentPanel = new JPanel();
        contentPanel.setLayout(new CardLayout());
        JPanel addAdminPage = new AddAdmin();
        JPanel adminListPage = new ListAdmin();
        contentPanel.add(addAdminPage, "addAdminPage");
        contentPanel.add(adminListPage, "adminListPage");
        showPanel("adminListPage");

        addAdminButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel( "addAdminPage");
            }
        });
        adminListButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("adminListPage");
            }
        });

        adminPanel.add(sidebar, BorderLayout.WEST);
        adminPanel.add(contentPanel, BorderLayout.CENTER);

        //返回主布局
        return adminPanel;
    }

    private static void showPanel(String panelName) {
        CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
        cardLayout.show(contentPanel, panelName);
    }
}
